package com.example.day05;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;

    FirstFragment fist;
    SecondFragment second;
    ThreeFragment three;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       btn1 =(Button)  findViewById(R.id.btn1);
       btn2 =(Button)  findViewById(R.id.btn2);
       btn3 =(Button)  findViewById(R.id.btn3);
       btn4 =(Button)  findViewById(R.id.btn4);

       btn1.setOnClickListener(this);
       btn2.setOnClickListener(this);
       btn3.setOnClickListener(this);
       btn4.setOnClickListener(this);
        fist = new FirstFragment();
        second = new SecondFragment();
        three = new ThreeFragment();

    }

    @Override
    public void onClick(View view) {
       Button v = (Button)view;
            switch (v.getId()){
                case R.id.btn1:
                    replaceFragment(fist);
                    break;
                case R.id.btn2:
                    replaceFragment(second);
                    break;
                case R.id.btn3:
                    System.out.println("按钮3");
                    replaceFragment(three);
                    break;
                case R.id.btn4:
                    System.out.println("hello bt4");
                    break;
            }



    }


    public void replaceFragment(Fragment fragment){
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = supportFragmentManager.beginTransaction();
        ft.replace(R.id.fm,fragment);
        ft.commit();
    }
}